﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FindMyChamp.ChampDataService;
using System.Device.Location;
using FindMyChamp.Helpers;

namespace FindMyChamp
{
    public class WPChamp : Champ
    {
        public WPChamp(Champ champ)
        {
            // set this objects properties to match what is passed in
            Update(champ);

            // subscribe once to geo for each wpchamp instance
            GeoData.PositionChanged += (s, e) =>
            {
                if (GeoData.Status == GeoPositionStatus.Ready)
                    UpdateDistance();
            };

            // set to current location
            UpdateDistance();
        }

        private void UpdateDistance()
        {
            if (GeoData.Status != GeoPositionStatus.Ready)
                return;
            // when position is updated, update distance
            var _FromLat = GeoData.Location.Latitude;
            var _FromLong = GeoData.Location.Longitude;
            var _ToLat = (double)Latitude;
            var _ToLong = (double)Longitude;
            this.DistanceAway = GeoData.GetDistance(_FromLat, _FromLong, _ToLat, _ToLong);
        }

        public void Update(Champ champ)
        {
            this.ID = champ.ID;
            this.FullName = champ.FullName;
            this.FirstName = champ.FirstName;
            this.LastName = champ.LastName;
            this.Email = champ.Email;
            this.Blog = champ.Blog;
            this.Twitter = string.IsNullOrWhiteSpace(champ.Twitter) ? "NONE" : "@" + champ.Twitter;
            this.Phone = champ.Phone;
            this.Latitude = champ.Latitude;
            this.Longitude = champ.Longitude;

            // update image path
            var _Image = string.IsNullOrWhiteSpace(champ.Image) ? "missing.png" : champ.Image;
            Image = string.Format("http://champ.blob.core.windows.net/champimages/{0}", _Image);

            // Use negative number so our VisibilityConverter hides the field in UI
            this.DistanceAway = -1.0;
        }

        // this is the same as PropertyChanged("Distance"), just easier to use
        public event EventHandler DistanceChanged;

        private double m_DistanceAway;
        public double DistanceAway
        {
            get { return m_DistanceAway; }
            set
            {
                m_DistanceAway = value;
                OnPropertyChanged("DistanceAway");
                OnPropertyChanged("IsLocal");
                if (DistanceChanged != null)
                    DistanceChanged(this, EventArgs.Empty);
            }
        }

        public bool IsLocal { get { return (DistanceAway > 0 && DistanceAway < 500); } }

        public GeoCoordinate Location
        {
            get
            {
                return new GeoCoordinate
                {
                    Longitude = (double)this.Longitude,
                    Latitude = (double)this.Latitude
                };
            }
        }
    }
}
